package com.ruoyi.ziran.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.ziran.domain.ZrPaperUser;
import com.ruoyi.ziran.service.IZrPaperUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import com.ruoyi.ziran.mapper.ZrReportMapper;
import com.ruoyi.ziran.domain.ZrReport;
import com.ruoyi.ziran.service.IZrReportService;
import com.ruoyi.common.core.text.Convert;

/**
 * 上报处理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-29
 */
@Service
public class ZrReportServiceImpl implements IZrReportService 
{
    @Autowired
    private ZrReportMapper zrReportMapper;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private IZrPaperUserService iZrPaperUserService;

    /**
     * 查询上报处理
     * 
     * @param id 上报处理主键
     * @return 上报处理
     */
    @Override
    public ZrReport selectZrReportById(Long id)
    {
        return zrReportMapper.selectZrReportById(id);
    }

    /**
     * 查询上报处理列表
     * 
     * @param zrReport 上报处理
     * @return 上报处理
     */
    @Override
    public List<ZrReport> selectZrReportList(ZrReport zrReport)
    {
        List<ZrReport> list=zrReportMapper.selectZrReportList(zrReport);
        list.forEach(item->{
            item.setSysUser(iSysUserService.selectUserById(item.getUserId()));
        });
        return list;
    }

    /**
     * 新增上报处理
     * 
     * @param zrReport 上报处理
     * @return 结果
     */
    @Override
    public int insertZrReport(ZrReport zrReport)
    {
        zrReport.setCreateTime(DateUtils.getNowDate());
        zrReport.setCreateBy(ShiroUtils.getUserId().toString());
        ZrPaperUser zrPaperUser =iZrPaperUserService.selectZrPaperUserById(zrReport.getPaperUserId());
        if (zrPaperUser==null){
            throw new RuntimeException("没有找到对应的试卷");
        }
        zrReport.setUserId(new Long(zrPaperUser.getAnswerUserId()));
        return zrReportMapper.insertZrReport(zrReport);
    }

    /**
     * 修改上报处理
     * 
     * @param zrReport 上报处理
     * @return 结果
     */
    @Override
    public int updateZrReport(ZrReport zrReport)
    {
        zrReport.setUpdateTime(DateUtils.getNowDate());
        return zrReportMapper.updateZrReport(zrReport);
    }

    /**
     * 批量删除上报处理
     * 
     * @param ids 需要删除的上报处理主键
     * @return 结果
     */
    @Override
    public int deleteZrReportByIds(String ids)
    {
        return zrReportMapper.deleteZrReportByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除上报处理信息
     * 
     * @param id 上报处理主键
     * @return 结果
     */
    @Override
    public int deleteZrReportById(Long id)
    {
        return zrReportMapper.deleteZrReportById(id);
    }
}
